package dhm.com.dhmshop.view.mine.myshop;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import dhm.com.dhmshop.R;
import dhm.com.dhmshop.adapter.mine.BillAdapter;
import dhm.com.dhmshop.adapter.mine.ShopOrderAdapter;
import dhm.com.dhmshop.base.BaseActiity;
import dhm.com.dhmshop.base.Presenter.PressenterImpl;
import dhm.com.dhmshop.base.netWork.Constant;
import dhm.com.dhmshop.base.netWork.LoginContract;
import dhm.com.dhmshop.entity.GetShopProfit;
import dhm.com.dhmshop.entity.ShopOrderBean;
import dhm.com.dhmshop.utils.SpUtils;

public class BillActivity extends BaseActiity implements LoginContract.IView {

    @BindView(R.id.time)
    TextView time;
    @BindView(R.id.money)
    TextView money;
    @BindView(R.id.recy_detail)
    XRecyclerView recyDetail;
    private int shop_id;
    private String uid;
    private int page;
    private BillAdapter shopOrderAdapter;
    private List<ShopOrderBean.DataBean> allOrder;

    private PressenterImpl pressenter;

    @Override
    protected int getLayout() {
        return R.layout.activity_bill;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        shop_id = getIntent().getIntExtra("shop_id", 1);
        uid = SpUtils.getString(this, "uid");
        pressenter = new PressenterImpl();
        pressenter.attachView(this);
        page = 1;

        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyDetail.setLayoutManager(manager);
        recyDetail.setLoadingMoreEnabled(true);
        recyDetail.setPullRefreshEnabled(true);
        recyDetail.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                page = 1;
                getOrder();
            }

            @Override
            public void onLoadMore() {
                getOrder();
            }
        });

    }

    private void getOrder() {

        Map<String, String> map = new HashMap<>();
        map.put("token", Constant.TOKEN);
        map.put("uid", uid);
        map.put("shop_id", shop_id + "");
        map.put("page", page + "");
        pressenter.sendMessage(this, Constant.AllShopOrders, map, ShopOrderBean.class);
    }


    @Override
    protected void initData() {
        Map<String, String> map = new HashMap<>();
        map.put("token", Constant.TOKEN);
        map.put("uid", uid);
        map.put("shop_id", shop_id + "");
        pressenter.sendMessage(BillActivity.this, Constant.ShopProfit, map, GetShopProfit.class);

    }

    @OnClick({R.id.back, R.id.time})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.time:
                Intent intent = new Intent(BillActivity.this, ChoosetimeActivity.class);
                intent.putExtra("shop_id", shop_id);
                startActivityForResult(intent, 200);
                break;
            default:
        }
    }

    @Override
    public void requesta(Object data) {
        if (data instanceof GetShopProfit) {
            GetShopProfit getShopProfit = (GetShopProfit) data;
            if (getShopProfit.getCode() == 1) {
                money.setText("￥"+getShopProfit.getData().get(0).getProfit());
            }
            getOrder();
        } else if (data instanceof ShopOrderBean) {
            ShopOrderBean shopOrderBean = (ShopOrderBean) data;
            if (shopOrderBean.getCode() == 1) {
                if (shopOrderBean.getData() != null && shopOrderBean.getData().size() > 0) {

                    for (int i = 0; i < shopOrderBean.getData().size(); i++) {
                        if (shopOrderBean.getData().get(i).getStatus() == 0||shopOrderBean.getData().get(i).getPay_status()==1||shopOrderBean.getData().get(i).getPay_status()==3) {
                            shopOrderBean.getData().remove(i);
                            i--;
                        }
                    }

                    if (page == 1) {
                        allOrder = shopOrderBean.getData();
                        shopOrderAdapter = new BillAdapter(this);
                        recyDetail.setAdapter(shopOrderAdapter);
                    } else {
                        allOrder.addAll(shopOrderBean.getData());
                    }
                    page++;
                    shopOrderAdapter.setShopList(allOrder);

                }
            } else {
                Toast.makeText(this, shopOrderBean.getMessage(), Toast.LENGTH_SHORT).show();
            }
            recyDetail.loadMoreComplete();
            recyDetail.refreshComplete();
        }


    }

    @Override
    public void fail(String error) {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 200:
                if (resultCode == RESULT_OK) {
                    String total = data.getStringExtra("total");
                    String time = data.getStringExtra("time");
                    money.setText(total);
                    this.time.setText(time);
                }
                break;
            default:
        }
    }
}
